package jp.heartdokyun.activity;

import java.util.ArrayList;
import java.util.HashMap;

import jp.heartdokyun.R;
import jp.heartdokyun.adapter.StageSelectionAdapter;
import jp.heartdokyun.listener.GoBackListener;
import jp.heartdokyun.listener.TopBackListener;
import jp.heartdokyun.model.Level;
import jp.heartdokyun.util.Utils;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class StageSelectionActivity extends Activity {
	private ArrayList<Level> mListLevel;
	private SoundPool mSoundPool;
	private HashMap<Integer, Integer> soundPoolMap;

	private MediaPlayer mMediaPlayer;
	private ListView lvStageSelection;
	private String stageType;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.stage_selection);
		initSounds();
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		TextView tvScreenTitle = (TextView) findViewById(R.id.tv_stage_collection_title);
		tvScreenTitle.setText(getResources().getString(R.string.game_tilte)
				+ " " + getIntent().getStringExtra("CHALLENGE_TYPE"));
		stageType = getIntent().getStringExtra("CHALLENGE_TYPE_VALUE");
		// String level = getIntent().getStringExtra("LEVEL");
		lvStageSelection = (ListView) findViewById(R.id.lv_stage_selection);

		String imageId = stageType + "_1";

		ImageView ivTypeImg = (ImageView) findViewById(R.id.iv_game_type_img);
		ivTypeImg.setImageResource(getResources().getIdentifier(imageId,
				"drawable", "jp.heartdokyun"));

		// mListLevel = new ArrayList<Level>();
		// mListLevel = Utils.getDataObj();
		// if (!Utils.isExistType(stageType)) {
		Utils.addLevel(this, stageType);
		mListLevel = Utils.getListLevelFromType(stageType);

		StageSelectionAdapter ssAdapter = new StageSelectionAdapter(this,
				mListLevel);
		lvStageSelection.setAdapter(ssAdapter);

		ImageButton ibBack = (ImageButton) findViewById(R.id.ib_stage_selection_back);
		ibBack.setOnClickListener(new GoBackListener(this));

		ImageButton ibo = (ImageButton) findViewById(R.id.ic_o);
		ibo.setOnClickListener(new TopBackListener(this));

//		new Thread(new Runnable() {
//
//			@Override
//			public void run() {
//				try {
//					Thread.sleep(2000);
//				} catch (InterruptedException e) {
//					e.printStackTrace();
//				}
//				playSound(1);
//			}
//		}).run();

		mMediaPlayer = MediaPlayer.create(getApplicationContext(),
				R.raw.other_bgm);
		float volume = Utils.getBGMVolume(getApplicationContext());
		mMediaPlayer.setVolume(volume, volume);
		mMediaPlayer.setLooping(true);
	}

	private void initSounds() {
		mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
		soundPoolMap = new HashMap<Integer, Integer>();
		soundPoolMap.put(1, mSoundPool.load(getApplicationContext(),
				R.raw.reberuwoerandene, 1));
	}

	@Override
	public void onDestroy() {
		mSoundPool.release();
		mMediaPlayer.release();
		super.onDestroy();
	}

	@Override
	public void onResume() {
		if (MainActivity.isEnableBGM) {
			mMediaPlayer.start();
		} else {
			if (mMediaPlayer.isPlaying()) {
				mMediaPlayer.pause();
				mMediaPlayer.seekTo(0);
			}
		}
		Utils.addLevel(this, stageType);
		mListLevel = Utils.getListLevelFromType(stageType);
		StageSelectionAdapter ssAdapter = new StageSelectionAdapter(this,
				mListLevel);
		lvStageSelection.setAdapter(ssAdapter);
		super.onResume();
	}

	@Override
	public void onPause() {
		if (mMediaPlayer.isPlaying()) {
			mMediaPlayer.pause();
			mMediaPlayer.seekTo(0);
		}
		super.onPause();
	}

	@Override
	public void onStart() {
		playSound(1);
		super.onStart();
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getAction() == KeyEvent.ACTION_DOWN
				&& event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			return true;
		}
		return false;
	}

	public void playSound(int sound) {
		float volume = Utils.getVoiceVolume(getApplicationContext());
		mSoundPool.play(soundPoolMap.get(sound), volume, volume, 1, 0, 1f);
	}
}